package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.entity.PyqAct;
import com.qianniu.napi.admin.entity.User;
import com.qianniu.napi.admin.service.IPyqActService;
import com.qianniu.napi.admin.service.IRedisService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * <p>
 * </p>
 *
 * @author chenxuebin
 * @since 2018-12-17
 */
@RestController
public class PyqActController {

    private static final Logger logger = LoggerFactory.getLogger(PyqActController.class);

    @Autowired
    private IPyqActService pyqactService;
    @Autowired
    private IRedisService redisService;

    @RequestMapping("/api/pyqact/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value = "status", required = false) String status,
                      @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                      @RequestParam(value = "pageSize", required = false) Integer pageSize,
                      HttpServletRequest req) {
        Jinfo info = new Jinfo();
        if (pageSize == null) pageSize = 10;
        Page page = new Page(pageIndex, pageSize);
        page = pyqactService.selectPage(page, new EntityWrapper<PyqAct>()
                .eq("uid", ObjectUtil.getUid())
                .eq("state", 1));
        info.setInfo(page.getRecords());
        return info;
    }

    @RequestMapping("/api/pyqact/add")
    @ResponseBody
    public Jinfo pyqactAdd(@Valid PyqAct pyqact) {
        Jinfo info = new Jinfo();
        // 字段校验

        pyqact.setCreatetime(new Date());
        pyqact.setMid(ObjectUtil.getMid());
        pyqact.setUid(ObjectUtil.getUid());
        pyqact.setState(1);

        User user = redisService.getUser();
        pyqact.setName(user.getNickName());
        pyqact.setLogo(user.getAvatarPic());

        pyqact.insert();
        return info;
    }

    @RequestMapping("/api/pyqact/modify")
    @ResponseBody
    public Jinfo pyqactUpdate(HttpServletRequest req,
                              PyqAct pyqact) {
        Jinfo info = new Jinfo();

        pyqact.updateById();
        return info;
    }

    @RequestMapping("/api/pyqact/detail")
    @ResponseBody
    public Jinfo getPyqActDetailById(@RequestParam("token") String token,
                                     @RequestParam("id") long id, HttpServletRequest req
    ) {
        Jinfo info = new Jinfo();
        PyqAct pyqact = pyqactService.selectById(id);
        info.setInfo(pyqact);
        return info;
    }
}
